$(function () {
    $(".loadingMarsk").css({
        "width":window.screen.availWidth,
        "height":window.screen.availHeight,
    });
    var token = localStorage.token;
    if(!token) {
        location.href = "../loginAndReg/login.html";
        return false;
    }
    if(parseInt(localStorage.editAddressStatus)===1) {
        $(".header span").html("修改地址");
    }
    if(parseInt(localStorage.addAddressStatus)===1) {
        $(".header span").html("添加地址");
    }
    new FastClick(document.body);
    var flag = false;
    $(".label").on("click", function () {
        flag = !flag;
        if (flag) {
            $(".label img").attr("src", "../../static/img/ico_select@3x.png");
            $(".adminAdd").css({ "color": "#1f1f1f" });
        } else {
            $(".label img").attr("src", "../../static/img/ico_selectk@3x.png");
            $(".adminAdd").css({ "color": "#a3a3a3" });
        }
    });
    //编辑地址
    var status = parseInt(localStorage.editStatus);
    var addressId = parseInt(localStorage.editAddressId);
    var regionId = parseInt(localStorage.editRegionId);
    var contact = localStorage.editContact;
    var mobile = localStorage.editMobile;
    var address = localStorage.editAddress;
    var region = localStorage.editRegion;
    localStorage.editRegion = "";
    if (status === 1) {
        $("#adminAdress")[0].checked = false;
        flag = true;
        $(".label img").attr("src", "../../static/img/ico_select@3x.png");
        $(".adminAdd").css({ "color": "#1f1f1f" });
    }
    if (status === 0) {
        $("#adminAdress")[0].checked = true;
        $(".label img").attr("src", "../../static/img/ico_selectk@3x.png");
        $(".adminAdd").css({ "color": "#a3a3a3" });
    }
    if (contact) $(".catchName").val(contact);
    if (mobile) $(".tel").val(mobile);
    if (region) {
        $(".area").html(region);
    } else {
        $(".area").html(localStorage.area);
    }
    if (address) $(".address").val(address);
    //选择地区
    localStorage.area = "";
    $(".region").on("click", function () {
        location.href = "./selectArea.html";
    });
    //添加新地址
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".save").on("click", function () {
        var contact = $(".catchName").val();
        if (!contact) {
            tips("请输入收货人");
            return false;
        }
        var mobile = $(".tel").val();
        if (!mobile) {
            tips("请输入联系电话");
            return false;
        }
        if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile))){ 
            tips("请输入正确的手机号码");
            return false; 
        } 
        var area = $(".area").html();
        if (!area) {
            tips("请选择所在地区");
            return false;
        }
        var address = $(".address").val();
        if (!address) {
            tips("请输入详细地址");
            return false;
        }
        if(address.length<5) {
            tips("详细地址不少于5个字");
            return false;
        }
        //获取选择地区的id
        if (localStorage.areaId) {
            var tempArr = localStorage.areaId.trim().split(" ");
            localStorage.areaId = "";
            regionId = parseInt(tempArr[tempArr.length - 1]);
        }
        var defaultVal = flag;
        if (defaultVal) {
            defaultVal = 1;
        } else {
            defaultVal = 0;
        }
        $.ajax({
            type: "post",
            url: domain.test + "/address",
            // url: domain.online + "/address/save",
            data: {
                id: addressId || "",
                contact: contact,
                mobile: mobile,
                address: address,
                regionId: regionId,
                default: defaultVal
            },
            beforeSend: function (xhr) {
                $(".loadingMarsk").css("background-color","rgba(0,0,0,0.3)");                
                $(".loadingMarsk").css("display","block");
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                // if (response.rtnCode === 200) {
                    $(".loadingMarsk").css("display","none");
                    if(parseInt(localStorage.editAddressStatus)===1) {
                        localStorage.editAddressStatus = "";
                        tips("修改成功，正在为你跳转");
                    }
                    if(parseInt(localStorage.addAddressStatus)===1) {
                        localStorage.addAddressStatus = "";
                        tips("地址添加成功，正在为你跳转");
                    }
                    setTimeout(function(){
                        location.href = "./address.html";
                    },1000)
                }
                if(response.data.rtnCode===400) {
                // if(response.rtnCode===400) {
                    tips(response.data.msg);
                    // tips(response.msg);
                }
            }
        });
    });
});